﻿using System;
using System.Collections;
using Col = Column<string, System.Type, int>;
using Telerik.Web.UI;
using System.Data;
using Guru.Data;
using Guru.Web.Controls.Telerik;
using Guru.Web.Controls.Asp;

public partial class FE_Customers : HSMPage
{
    protected const string LINK_CUSTOMER_DETAIL = "<a class='rgLinkIcon rgLinkEdit' onclick=\"OpenPopupModal(1, 'CustomerDetails.aspx?{1}');\">{0}</a>";

    protected void DoSearchClick(object sender, EventArgs e)
    {
        this.uxCustomersGrid.Rebind();       
    }
   
    protected void uxCustomersGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@GuestName", this.uxFiltering.GuestName, DbType.String);
        parameters.Add("@IDNumber", this.uxFiltering.IdNumber, DbType.String);
        parameters.Add("@VisaNumber", this.uxFiltering.VisaPasswort, DbType.String);
        parameters.Add("@Address", this.uxFiltering.Address, DbType.String);
        parameters.Add("@Phone", this.uxFiltering.Phone, DbType.String);
        parameters.Add("@Email", this.uxFiltering.Email, DbType.String);
        parameters.Add("@Keywords", this.uxFiltering.Keywords, DbType.String);

        uxCustomersGrid.DataSource = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Guest_GetAll", parameters);        
    }
    protected void uxCustomersGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            DataRowView dataView = e.Item.DataItem as DataRowView;
            string qString = BuildSecureQueryString("GuestID=" + dataView["GuestID"].ToString());
            dataItem["GuestCode"].Text = string.Format(LINK_CUSTOMER_DETAIL, dataView["GuestCode"], qString);
        }
    }

    protected void uxCustomersGrid_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            GridDataItem item = e.Item as GridDataItem;
            int id = int.Parse(item.GetDataKeyValue("GuestID").ToString());           
            GuruParameterCollection parameters = new GuruParameterCollection();
            GuruParameterCollection outparameters = new GuruParameterCollection();
            parameters.AddUserLoggedParams();
            parameters.Add("@GuestID", id, DbType.Int32);
            parameters.Add("@Result", 0, DbType.Int32, true);
            HSMCommon.HSMBusiness.UpdateData("spa_HSM_Guest_Delete", parameters, out outparameters);
            int resultCode = int.Parse(outparameters[0].ParameterValue.ToString());
            if (resultCode == 0)
            {
                this.uxCustomersGrid.Rebind();
            }
            else 
            {
                this.AlertClient(Resources.MessageManager.CannotDeleteCustomer);
            }
        }
    }
    

}
